# -*- coding:utf-8 -*-

from fastapi import APIRouter, Depends, Form
from sqlalchemy.ext.asyncio import AsyncSession
from starlette.requests import Request
from typing import List
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.entity.vo.import_vo import ImportModel
from module_admin.service.import_service import ImportService
from module_admin.service.login_service import LoginService
from module_admin.aspect.data_scope import GetDataScope
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.annotation.log_annotation import Log
from utils.response_util import ResponseUtil
from utils.common_util import bytes2file_response

from module_doc.entity.vo.bg_file_vo import BgFilePageModel, BgFileModel
from module_doc.service.bg_file_service import BgFileService

bgFileController = APIRouter(prefix='/bg/file')


@bgFileController.get('/list')
async def get_bg_file_list(
        request: Request,
        query_db: AsyncSession = Depends(get_db),
        page_query: BgFilePageModel = Depends( BgFilePageModel.as_query)
):
    bg_file_result = await BgFileService.get_bg_file_list(query_db, page_query, None)

    return ResponseUtil.success(model_content=bg_file_result)

@bgFileController.get('/getById/{bgFileId}')
async def get_bg_file_by_id(
        request: Request,
        bgFileId: int,
        query_db: AsyncSession = Depends(get_db)
):
    bg_file = await BgFileService.get_bg_file_by_id(query_db, bgFileId)
    return ResponseUtil.success(data=bg_file)


@bgFileController.get('/download/{bgFileId}', dependencies=[Depends(CheckUserInterfaceAuth('bg:file:query'))])
async def download_file_by_id(
        request: Request,
        bgFileId: int,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    client_host = request.headers.get("x-forwarded-for")
    if client_host:
        ip = client_host.split(",")[0].strip()
    else:
        ip = request.client.host
    bg_file = await BgFileService.download_by_file_id(query_db, current_user.user, bgFileId, ip)
    return ResponseUtil.success(data=bg_file)


@bgFileController.post('/add', dependencies=[Depends(CheckUserInterfaceAuth('bg:file:add'))])
@Log(title='bg_file', business_type=BusinessType.INSERT)
async def add_bg_file (
    request: Request,
    add_model: BgFileModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):

    add_model.create_by = current_user.user.user_id
    add_model.dept_id = current_user.user.dept_id
    add_dict_type_result = await BgFileService.add_bg_file(query_db, add_model)
    return ResponseUtil.success(data=add_dict_type_result)

@bgFileController.put('/update', dependencies=[Depends(CheckUserInterfaceAuth('bg:file:edit'))])
@Log(title='bg_file', business_type=BusinessType.UPDATE)
async def update_bg_file(
    request: Request,
    edit_model: BgFileModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_dict_type_result = await BgFileService.update_bg_file(query_db, edit_model)
    return ResponseUtil.success(data=add_dict_type_result)


@bgFileController.delete('/delete/{bgFileIds}', dependencies=[Depends(CheckUserInterfaceAuth('bg:file:remove'))])
@Log(title='bg_file', business_type=BusinessType.DELETE)
async def del_bg_file(
    request: Request,
    bgFileIds: str,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    ids = bgFileIds.split(',')
    del_result = await BgFileService.del_bg_file(query_db, ids)
    return ResponseUtil.success(data=del_result)

@bgFileController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('bg:file:export'))])
@Log(title='bg_file', business_type=BusinessType.EXPORT)
async def export_bg_file(
    request: Request,
    bg_file_form: BgFilePageModel = Form(),
    query_db: AsyncSession = Depends(get_db),
    data_scope_sql: str = Depends(GetDataScope('BgFile')),
):
    # 获取全量数据
    export_result = await BgFileService.export_bg_file_list(
        query_db, bg_file_form, data_scope_sql
    )
    return ResponseUtil.streaming(data=bytes2file_response(export_result))

@bgFileController.post('/import', dependencies=[Depends(CheckUserInterfaceAuth('bg:file:import'))])
async def import_bg_file(request: Request,
                      import_model: ImportModel,
                      query_db: AsyncSession = Depends(get_db),
                      current_user: CurrentUserModel = Depends(LoginService.get_current_user)
    ):
    """
    导入数据
    """
    await ImportService.import_data(query_db, import_model, current_user)
    return ResponseUtil.success()